package myjava3d;

import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class MyScreen extends JFrame {
	public Canvas3D myCanvas3D; //= Leinwand

	private static final long serialVersionUID = 1L;
	
	public MyScreen() {
		
		//Standardeinstellung fuer Universum
		myCanvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
		
		//Universum mit Canvas verbinden
		SimpleUniverse simpUniv = new SimpleUniverse(myCanvas3D);
		
		//Betrachterpunkt umkreist NullPunkt
		simpUniv.getViewingPlatform().setNominalViewingTransform();
		
		//Die Szene wird in dieser Methode erzeugt.
		//Modell erzeugen und zum Universe hinzufuegen
		SceneBackground background = new SceneBackground();
		Ground ground = new Ground();
		R2D2 r2d2 = new R2D2();
		VrmlObject legoMaennchen = new VrmlObject();
		
		
		Transform3D transR2d2 = new Transform3D();
		transR2d2.setTranslation(new Vector3f(0.4f, 0.35f, 0.0f));
		TransformGroup transR2d2Group = new TransformGroup(transR2d2);
		transR2d2Group.addChild(r2d2);
		BranchGroup transformedR2d2 = new BranchGroup();
		transformedR2d2.addChild(transR2d2Group);
		
		Transform3D transLegoMaennchen = new Transform3D();
		transLegoMaennchen.setTranslation(new Vector3f(-0.4f, 0.05f, 0.0f));
		TransformGroup transLegoMaennchenGroup = new TransformGroup(transLegoMaennchen);
		transLegoMaennchenGroup.addChild(legoMaennchen);
		BranchGroup transformedLegomaennchen = new BranchGroup();
		transformedLegomaennchen.addChild(transLegoMaennchenGroup);
		
		
		//Simple Pick Behaviour
	    SimplePickRightBehaviour simplePick = new SimplePickRightBehaviour(myCanvas3D, r2d2, new BoundingSphere());
	    transformedR2d2.addChild(simplePick);
	    transformedR2d2.compile();
		
	    //Hinzufuegen zum Universum
		simpUniv.addBranchGraph(background);
		simpUniv.addBranchGraph(ground);
	    simpUniv.addBranchGraph(transformedR2d2);
	    simpUniv.addBranchGraph(transformedLegomaennchen);
	    
	    
	    
	    //Hinzufuegen von Licht
	    MyLight light = new MyLight();
	    simpUniv.addBranchGraph(light.getBranchGroup());
	    
	    
	    
	    //Hierdurch kann man mit der Maus den Betrachterstandpunkt veraendern
	    OrbitBehavior ob = new OrbitBehavior(myCanvas3D);
	    ob.setSchedulingBounds(new BoundingSphere(new Point3d(0.0,0.0,0.0),Double.MAX_VALUE));
	    simpUniv.getViewingPlatform().setViewPlatformBehavior(ob);
	}
}
